<?php
/*
 * Created on 08.03.2006
 *
 */


/**
 * BundleTool combines all methods used for message bundle loading and compilation
 */
class phpinchains_util_BundleTool
{
    
    /**
     * Load a bundle from bundle folder
     * 
     * @param string bundlefile Absolute filename
     * @param string locale Locale to load from bundle
     * @return array Bundle as array
     */
    function loadBundle($bundlefile, $locale, $bindlocale = false)
    {
        if ($locale && file_exists($bundlefile."_".$locale.".bundle"))
        {
            $bundlefile = $bundlefile."_".$locale.".bundle";
        }
        elseif (file_exists($bundlefile.".bundle"))
        {
            $bundlefile = $bundlefile.".bundle";
        }
        else
        {
            print "Error: Bundle file $bundlefile not found!";
            return;
        }
        if (file_exists($bundlefile.'.php'))
        {
            $cfile = stat($bundlefile.'.php');
            $sfile = stat($bundlefile);
            // mtime comparison
            if ($cfile[9] < $sfile[9])
            {
                phpinchains_util_BundleTool::compileBundle($bundlefile);
            }                
        }
        else // if compiled php file does not exists, create it anyway
        {
            phpinchains_util_BundleTool::compileBundle($bundlefile);
        }
        include ($bundlefile.'.php');
        return $bundle;
    }
    
    function compileBundle ($bundlename)
    {
        m2import('Config.php');
        $phpIni = new Config();
        $root =& $phpIni->parseConfig($bundlename, 'inicommented');
        if (PEAR::isError($root)) {
            die($root->getMessage());
        }
        $fp = fopen($bundlename.'.php', 'w');
        fputs($fp, '<'."?php\n".$root->toString('phparray', array('name' => 'bundle'))."\n".'?>');
        fclose($fp);
    }
    
}

?>
